<template>
  <div class="content">
    <!--    仓库列表弹窗-->
    <jc-popup
      v-model="warehouseName"
      :dialog-title="$t('menu.stockList')"
      :popup-title="popupTitle"
      :open-dialog="openWarehouse"
      @closeDialog="closeDialog"
      @emptyForm="emptyForm"
      @handleSearch="searchWarehouse"
    >
      <template v-slot:content>
        <div>
          <jc-table
            serial
            :table-data="warehouseData"
            :table-header="warehouseHeader"
            table-height="500px"
            @clickRow="warehouseSelectRow"
          />
        </div>
      </template>
      <template v-slot:pagination>
        <jc-pagination
          v-show="warehousePagination.total > 0"
          :total="warehousePagination.total"
          :page.sync="warehousePagination.pageNum"
          :limit.sync="warehousePagination.pageSize"
          @pagination="handleWarehouse"
        />
      </template>
    </jc-popup>
  </div>
</template>
<script>
import jcPopup from '@/views/components/Popup/index.vue'
import { queryTBdStock, queryStockQtyList, queryQtyWithStock } from '@/api/purchaseManagement/createPurchasePrice'
export default {
  components: {
    jcPopup
  },
  props: {
    fmaterialId: {
      type: String,
      default: ''
    },
    // 有 fmaterialId 条件下 1正常仓库, 2其他调拨单转入仓库
    msg: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      // 仓库
      warehouseName: '', // 仓库关键词
      openWarehouse: true, // 仓库弹窗
      dialogTitle: '仓库列表', // 弹窗标题
      popupTitle: this.$t('shop.name1'), // 名称 查询条件文本
      warehouseData: [],
      warehouseHeader: [
        { label: this.$t('shop.fnumber'), prop: 'fnumber' },
        { label: this.$t('market.stockname'), prop: 'fname' },
        { label: this.$t('market.address'), prop: 'faddress' },
        // { label: this.$t('market.principal'), prop: 'fprincipal' },
        { label: this.$t('market.inventoryQty'), prop: 'sumQty' },
        { label: this.$t('market.funit'), prop: 'funitName' }
      ],
      warehousePagination: {
        total: 0, // 总条目
        pageNum: 1, // 当前页
        pageSize: 200 // 每页显示多少条数据
      },
      prodValue: { CKID: '', ckName: '', selecelDialog: true }
    }
  },
  created() {
    this.handleWarehouse()
  },
  methods: {
    // 仓库列表
    async handleWarehouse() {
      if (this.fmaterialId) { // 1显示有库存-仓库, 2其他调拨单转入仓库(所有仓库)
        const api = this.msg === 1 ? queryStockQtyList : queryQtyWithStock
        const { data: RES } = await api({ ...this.warehousePagination,
          fnumber: this.fmaterialId, fstockName: this.warehouseName })
        this.warehouseData = RES.list
        this.warehouseData.map(res => {
          res.fname = res.fstockName
          res.fnumber = res.stockFnumber || res.fstockFnumber
        })
        this.warehousePagination.total = RES.total
      } else { // 全部仓库
        const { data: RES } = await queryTBdStock({ ...this.warehousePagination, fstockName: this.warehouseName })
        this.warehouseData = RES.list
        this.warehousePagination.total = RES.total
      }
    },
    // 双击仓库弹窗
    warehouseSelectRow(item) {
      this.prodValue.ckid = item.fstockId
      this.prodValue.ck = item.fname
      this.prodValue.sumQty = item.sumQty
      this.prodValue.fnumber = item.fnumber
      this.$emit('client', item)
    },
    // 仓库弹窗搜索
    searchWarehouse() {
      this.warehousePagination.pageNum = 1
      this.handleWarehouse()
    },
    // 弹窗关闭事件
    closeDialog() {
      this.warehousePagination.pageNum = 1
      this.$emit('client', this.prodValue)
    },
    // 清空弹窗表单
    emptyForm() {
      this.warehouseName = ''
    }
  }
}
</script>

<style scoped lang="scss">
.content {
  @include listBom;
}
</style>
